package com.shujia.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.ArrayList;

public class FileRead {
    /**
     * 读取文件工具
     *
     * @param path 文件路径
     * @return 返回一个集合
     */
    public static <T> ArrayList<T> read(String path, Class<T> tClass) {

        ArrayList<T> list = new ArrayList<T>();

        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        try {

            //2、创建hdfs文件输入流
            URI uri = new URI("hdfs://node1:9000");
            Configuration configuration = new Configuration();
            FileSystem fileSystem = FileSystem.get(uri, configuration);
            FSDataInputStream inputStream = fileSystem.open(new Path(path));
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);

            bufferedReader = new BufferedReader(inputStreamReader);


            //按行读取数据
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                String[] split = line.split(",");


                /**
                 * 构建T类型的对象
                 */
                T t = tClass.newInstance();
                //对属性进行赋值

                //1、获取所有属性
                Field[] fields = tClass.getDeclaredFields();

                for (int i = 0; i < fields.length; i++) {
                    Field field = fields[i];

                    String fieldName = field.getName();

                    Class<?> type = field.getType();

                    //构建属性设置值得方法名  setName   name   setName
                    String methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);


                    if (type == Integer.class) {
                        //获取方法对象
                        Method method = tClass.getMethod(methodName, Integer.class);
                        //执行方法
                        method.invoke(t, Integer.parseInt(split[i]));
                    } else {
                        //获取方法对象
                        Method method = tClass.getMethod(methodName, String.class);
                        //执行方法
                        method.invoke(t, split[i]);
                    }


                }


                list.add(t);
            }


        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            //关闭流
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }


        return list;

    }

}
